<template>
  <div class="wrapper">
    <div
      style="margin: 70px auto;background-color: #fff;width: 350px;height: 500px;padding: 20px;border-radius: 10px;">
      <div style="margin: 20px 0;text-align: center;font-size: 20px;">
        <b>用户注册</b>
      </div>
      <el-form :model="user" :rules="rules" ref="userForm">
        <el-form-item prop="username">
          <el-input size="medium" placeholder="用户名" style="margin: 10px 0" prefix-icon="el-icon-user"
                    v-model="user.username"></el-input>
        </el-form-item>
        <el-form-item prop="phone">
          <el-input size="medium" placeholder="手机号" style="margin: 10px 0" prefix-icon="el-icon-phone"
                    v-model="user.phone"></el-input>
        </el-form-item>
        <el-form-item prop="email">
          <el-input size="medium" placeholder="绑定邮箱" style="margin: 10px 0" prefix-icon="el-icon-paperclip"
                    v-model="user.email"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input size="medium" placeholder="设置密码" style="margin: 10px 0" prefix-icon="el-icon-lock" show-password
                    v-model="user.password"></el-input>
        </el-form-item>
        <el-form-item prop="checkPass">
          <el-input size="medium" placeholder="确认密码" style="margin: 10px 0" prefix-icon="el-icon-lock" show-password
                    v-model="user.checkPass"></el-input>
        </el-form-item>
        <el-form-item style="margin: 50px 0;text-align:center;">
          <el-button type="primary" text-align:left size="big" autocomplete="off" @click="register">确定注册</el-button>
          <el-dialog
            title="恭喜你，注册成功！"
            :visible.sync="centerDialogVisible"
            width="30%"
            center>
            <span slot="footer" class="dialog-footer">
              <el-button @click="centerDialogVisible = false">返回登录</el-button>
              <el-button type="primary" @click="centerDialogVisible = false;$router.push('/manage')">快速登录</el-button>
            </span>
          </el-dialog>
          <el-button type="warning" text-align:right size="big" autocomplete="off" @click="$router.push('/login')">返回登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>

</template>

<script>
import {register} from "@/api/user.js";

export default {
  name: "Register",
  data() {
    return {
      user: {},
      centerDialogVisible: false,
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
        ],
        phone: [
          {required: true, message: '请输入手机号', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ],
        email: [
          {required: true, message: '请输入邮箱', trigger: 'blur'}
        ],
        checkPass: [
          {required: true, message: '请输入重新输入密码', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    //后台接口,Post请求
    register() {
      this.$refs["userForm"].validate((valid) => {
        if (valid) { //表单校验合法
          if (this.user.password !== this.user.checkPass) {
            this.$message.error("两次输入的密码不一致")
            return false
          }
          register(this.user).then((res) => {
            console.log(response);
            if (res.code === "20000") {
              this.centerDialogVisible = true
              this.$message.success("注册成功")
            } else {
              this.$message.error(res.msg)
            }
          });
          console.log("send succeed")
          return true;
        } else {
          console.log('error submit!!');
          return false;
        }


      });
    }
  }
}
</script>
<style>
.wrapper {
  height: 120vh;
  background-image: linear-gradient(to bottom right, #363445, #263445);
  /*超出的部分隐藏*/
  overflow: hidden;
}
</style>
